package cn.itcast.web.shiro;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.apache.shiro.crypto.hash.Md5Hash;

public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {

    /*
          token: token包含了用户输入的密码与邮箱。
          AuthenticationInfo ：  包含了数据库中密码

       */
    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        //1. 获取用户输入明文的邮箱与密码
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        String email = usernamePasswordToken.getUsername();
        String password = new String(usernamePasswordToken.getPassword());

        //2.使用shiro的工具对用户输入的明文密码进行加盐加密
        Md5Hash md5Hash = new Md5Hash(password,email);
        String saltPassword = md5Hash.toString(); //加盐后的密码


        //3. 使用用户加盐加密后密码与数据库的密码进行对比
        String dbPassowrd = (String) info.getCredentials(); //获取数据库中密码
        return saltPassword.equals(dbPassowrd);

    }
}
